/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import hyper.market.beans.StockBean;
import hyper.market.beans.StoreBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Mikeno
 */
public class CreationOfStockServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String[] stores = request.getParameterValues("checkstore");
        String mySqlJndiName = this.getServletContext().getInitParameter("MySql JNDI");
        String itemName = request.getParameter("itemname");
        int threshold = 0;
        double selling = 0,discount = 0,cost = 0;
        int quantity = 0;
        String image = request.getParameter("imageNameH");
        String expiry = request.getParameter("expiry");
      
        if (itemName.equals("") || itemName == null || expiry.equals("Show Calendar") || expiry.equals("") ||expiry == null || image.equals("") || image== null) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<script language=\"JavaScript\" type=\"text/javascript\">");
            out.println("function validatefail(){");
            out.println("alert(Please Enable your Javascript);");
            out.println("history.back();");
            out.println("}");
            out.println("</script>");
            out.println("</head>");
            out.println("<body onload=\"validatefail()\"></body>");
            out.println("</html>");
        }
        try {
            discount = Double.parseDouble(request.getParameter("discount"));
            quantity = Integer.parseInt(request.getParameter("quantity"));
            threshold = Integer.parseInt(request.getParameter("threshold"));
            cost = Double.parseDouble(request.getParameter("cost"));
            selling = Double.parseDouble(request.getParameter("selling"));
        } catch (NumberFormatException e) {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("<html>");
            out.println("<head>");
            out.println("<script language=\"JavaScript\" type=\"text/javascript\">");
            out.println("function validatefail(){");
            out.println("alert(Please Enable your Javascript);");
            out.println("history.back();");
            out.println("}");
            out.println("</script>");
            out.println("</head>");
            out.println("<body onload=\"validatefail()\"></body>");
            out.println("</html>");
        }


        StockBean stock = new StockBean(itemName, cost, selling, quantity, threshold, image, discount, expiry.toString());
        StoreBean store = new StoreBean();
        ArrayList templist = stock.createStock(mySqlJndiName, stores, stock);
        ArrayList errorlist = null;
        ArrayList nameList = new ArrayList();

        if (!templist.isEmpty()) {
            errorlist = store.retrieveStoreName(mySqlJndiName, templist);
            request.getSession().setAttribute("errorlist", errorlist);
        }

        for (int i = 0; i < stores.length; i++) {
            nameList.add(stores[i]);
        }
        templist = store.retrieveStoreName(mySqlJndiName, nameList);
        request.getSession().setAttribute("stock", stock);
        request.getSession().setAttribute("storenamelist", templist);

        request.getRequestDispatcher("/WEB-INF/CreationReport.jsp").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
